package com.jxb.myshop.common;

/**
 * @author: JXB
 * 响应结果
 * 统一返回数据类型
 * 方便前端开发 使用这个数据类型
 * 直接判断后台服务器响应的结果
 * 而不需要等待后台开发告诉前端会返回什么数据
 */
public class ResponseResult<T> {
    /**
     * 返回请求状态
     */
    private int code;
    /**
     * 返回请求操作信息
     */
    private String msg;
    /**
     * 设置泛型数据
     */
    private T data;

    @Override
    public String toString() {
        return "ResponseResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    /**
     * 
     * @return
     * 设置成功响应的信息
     */
    public static ResponseResult createSuccess(){
        return new ResponseResult(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg());
    }
    public static ResponseResult createSuccess(String msg){
        return new ResponseResult(ResponseCode.SUCCESS.getCode(), msg);
    }
    public static <T> ResponseResult createSuccess(T data){
        return new ResponseResult(ResponseCode.SUCCESS.getCode(), data);
    }
    public static ResponseResult createSuccess(int code){
        return new ResponseResult(code, ResponseCode.SUCCESS.getMsg());
    }public static <T> ResponseResult createSuccess(int code,String msg,T data){
        return new ResponseResult(code,msg,data);
    }

    /**
     * 设置失败响应
     */
    public static ResponseResult createFail(){
        return new ResponseResult(ResponseCode.FAIL.getCode(), ResponseCode.FAIL.getMsg());
    }
    public static ResponseResult createFail(String msg){
        return new ResponseResult(ResponseCode.FAIL.getCode(), msg);
    }
    public static <T> ResponseResult createFail(T data){
        return new ResponseResult(ResponseCode.FAIL.getCode(), data);
    }
    public static ResponseResult createFail(int code){
        return new ResponseResult(code, ResponseCode.FAIL.getMsg());
    }public static <T> ResponseResult createFail(int code,String msg,T data){
        return new ResponseResult(code,msg,data);
    }
    
    /**
     * 
     * @return
     * 下面是get/set 以及构造方法
     */
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public ResponseResult(int code) {
        this.code = code;
    }

    public ResponseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseResult(String msg, T data) {
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(T data) {
        this.data = data;
    }

    public ResponseResult() {
    }
}
